package threadPr;
 import java.awt.event.*;
 import java.util.*;
 import javax.swing.*;


 public class TheadGUI
 {
    public static void main(String[] args)
    {
       SwingThreadFrame frame = new SwingThreadFrame();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
    }
 }


 class SwingThreadFrame extends JFrame
 {
    public SwingThreadFrame()
    {
       setTitle("SwingThreadTest");

       final JLabel first=new JLabel("First");
       final JLabel second=new JLabel("Second");
       JPanel panel = new JPanel();

       JButton firstButton = new JButton("First");
       firstButton.addActionListener(new ActionListener()
          {
             public void actionPerformed(ActionEvent event)
             {
                new Thread(new FirstRunnable(first)).start();
             }
          });
      
       JButton secondButton = new JButton("Second");
       secondButton.addActionListener(new ActionListener()
          {
             public void actionPerformed(ActionEvent event)
             {
                new Thread(new SecondRunnable(second)).start();
             }
          }); 
       panel.add(firstButton);
       panel.add(secondButton);
       panel.add(first);
       panel.add(second);
       add(panel);
       pack();
    }
 }

 class FirstRunnable implements Runnable
 {
     
    public FirstRunnable(JLabel first)
    {

       l=first;
       generator = new Random();
    }

    public void run()
    {
       try
       {
          while (true)
          {             
             int i = Math.abs(generator.nextInt());
             l.setText("Bad:"+i/10000);
             Thread.sleep(1000);
          }
       }
       catch (InterruptedException e) {}
    }

    JLabel l;
    private Random generator;
 }

 class SecondRunnable implements Runnable
 {

    public SecondRunnable(JLabel second)
    {

       l=second;
       generator = new Random();
    }

    public void run()
    {
       try
       {
          while (true)
          {
             int i = Math.abs(generator.nextInt());
             l.setText("Good:"+i/10000);
             Thread.sleep(1000);
          }
       }
       catch (InterruptedException e) {}
    }

    JLabel l;
    private Random generator;
 }

